/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;

import DAO.LoaiDaiLyDAO;
import DTO.LoaiDaiLyDTO;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author HuuTri
 */
public class LoaiDaiLyBUS {
    
    /**
     * delete record by id
     * @param id
     * @param maloaidaily
     * @param tenloaidaily
     * @param mota
     * @return 
     */
    public static boolean deleteRecord(String id) {
        LoaiDaiLyDTO loaidaily = new LoaiDaiLyDTO();
        loaidaily.setId(Integer.parseInt(id));
        
        return LoaiDaiLyDAO.delete(loaidaily);
    }
    
    /**
     * update record by id
     * @param id
     * @param maloaidaily
     * @param tenloaidaily
     * @param mota
     * @return 
     */
    public static boolean updateRecord(String id, String maloaidaily,
                                       String tenloaidaily, String mota) {
        LoaiDaiLyDTO loaidaily = new LoaiDaiLyDTO(
                                             Integer.parseInt(id),
                                             maloaidaily,
                                             tenloaidaily,
                                             mota
                                                  );
        return LoaiDaiLyDAO.update(loaidaily);
    }
    
    /**
     * insert new record
     * @param id
     * @param maloaidaily
     * @param tenloaidaily
     * @param mota
     * @return 
     */
    public static boolean insertRecord(String id, String maloaidaily, 
                                        String tenloaidaily, String mota) {
         LoaiDaiLyDTO loaidaily = new LoaiDaiLyDTO(
                                             Integer.parseInt(id),
                                             maloaidaily,
                                             tenloaidaily,
                                             mota
                                                  );
        return LoaiDaiLyDAO.insert(loaidaily);
    }
    
    /**
     * set value to tableModel
     */
    public static void setValue(TableModel tableModel, int row, 
                                String columnName, Object value) {
        int index;
        if(row < 0) return;
        
        index = 0;
        switch (columnName) {
            case LoaiDaiLyDTO.ID: 
                tableModel.setValueAt(value, row, index);
                break;
            case LoaiDaiLyDTO.MA_LOAI_DAI_LY: 
                tableModel.setValueAt(value, row, index + 1);
                break;
            case LoaiDaiLyDTO.TEN_LOAI_DAI_LY: 
                tableModel.setValueAt(value, row, index + 2);
                break;
            case LoaiDaiLyDTO.MO_TA: 
                tableModel.setValueAt(value ,row, index + 3);
                break;
        }
    }
    
    /**
     * get Value from tablemodel
     * @param tableModel
     * @param row
     * @param columnName
     * @return 
     */
    public static Object getValue(TableModel tableModel, int row, String columnName) {
         int index = 0;
         Object value= "";
         if (row < 0) return null;
         
         switch (columnName) {
            case LoaiDaiLyDTO.ID: 
                value = tableModel.getValueAt(row, index);
                break;
            case LoaiDaiLyDTO.MA_LOAI_DAI_LY: 
                value = tableModel.getValueAt(row, index + 1);
                break;
            case LoaiDaiLyDTO.TEN_LOAI_DAI_LY: 
                value = tableModel.getValueAt(row, index + 2);
                break;
            case LoaiDaiLyDTO.MO_TA: 
                value = tableModel.getValueAt(row, index + 3);
                break;
         }
         if (value != null) 
            return value;
         else 
             return " ";
    }
    
    /**
     * get new MaLoaiDaiLy
     * @return 
     */
    public static int getNewMaLoaiDaiLy() {
        return LoaiDaiLyDAO.getNewMaLoaiDaiLy();
    }
    
    /**
     * check maloaidaily exist
     * @param id
     * @param maloaidaily
     * @return 
     */
    public static boolean checkMaLoaiDaiLy(String id, String maloaidaily) {
        LoaiDaiLyDTO loaidaily = new LoaiDaiLyDTO();
        loaidaily.setId(Integer.parseInt(id));
        loaidaily.setMaLoaiDaiLy(maloaidaily);
        return LoaiDaiLyDAO.checkMaLoaiDaiLy(loaidaily);
    }
    
    /**
     * get all data about LoaiDaiLy
     * @return 
     */
    public static DefaultTableModel getTableModel() {
        return LoaiDaiLyDAO.getTableModel();
    }
}
